package network.tcp.bio;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.net.Socket;
import java.util.UUID;

/**
 * @program: baseModule
 * @description:
 * @author: zhanghz001
 * @create: 2020-10-27 04:39
 **/
public class ThreadSocket implements Runnable {

    private final Socket accept;

    public ThreadSocket(Socket accept) {
        this.accept = accept;

    }

    @Override
    public void run() {
        try (
                BufferedOutputStream os = new BufferedOutputStream(accept.getOutputStream());
                BufferedOutputStream fos = new BufferedOutputStream(
                        new FileOutputStream(UUID.randomUUID().toString() + ".txt"));
                BufferedInputStream is = new BufferedInputStream(accept.getInputStream());
        ) {
            //服务端接收文件
            byte[] bytes = new byte[1024];
            int len;
            while ((len = is.read(bytes)) != -1) {
                fos.write(bytes, 0, len);
            }

            //服务端反馈
            os.write("服务端接收成功".getBytes());
            os.flush();
            accept.shutdownOutput();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
    }
}
